home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / main.c < prev    next >
C/C++ Source or Header  |  1994-04-23  |  11KB  |  353 lines

  1. /**********************************************************************\
  2.  
  3. File:        main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "graphics.h"
  25. #include "main.h"
  26. #include "apple events.h"
  27. #include "integrity.h"
  28. #include "about.h"
  29. #include "help.h"
  30. #include "menus.h"
  31. #include "prefs.h"
  32. #include "environment.h"
  33. #include "progress.h"
  34. #include "error.h"
  35. #include "sounds.h"
  36. #include "demo.h"
  37. #include "program globals.h"
  38.  
  39. void main(void)
  40. {
  41.     Boolean            programIntegrityVerified;
  42.     Boolean            programIntegritySet;
  43.     
  44.     /* do integrity check before anything else; see integrity.c for details */
  45.     programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
  46.     
  47.     /* standard program initialization stuff */
  48.     MaxApplZone();    
  49.     InitGraf(&thePort);
  50.     InitFonts();
  51.     FlushEvents(everyEvent, 0);
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(0L);
  56.     InitCursor();
  57.     GetDateTime(&randSeed);
  58.     
  59.     if (!InitTheEnvironment())            /* gestalt checks and variable initialization */
  60.         HandleError(kSystemTooOld, TRUE);        /* less than system 4.1 */
  61.     
  62.     if (!programIntegrityVerified)    /* integrity check failed */
  63.         HandleError(kProgramIntegrityNotVerified, TRUE);
  64.     
  65.     if (programIntegritySet)    /* integrity check freshly installed */
  66.         HandleError(kProgramIntegritySet, FALSE);
  67.     
  68.     if (!InitTheMenus())        /* get menus from .rsrc and draw menu bar */
  69.         HandleError(kProgramIntegrityNotVerified, TRUE);
  70.         
  71.     if (!InitTheGraphics())        /* initialize offscreen gworlds/bitmaps, etc */
  72.         HandleError(kNoMemoryAndQuitting, TRUE);
  73.     
  74.     InitTheSounds();                /* see if sound is available, etc. */
  75.     
  76.     PrefsError(PreferencesInit());    /* get prefs (create if necessary) */
  77.     
  78.     InitTheProgram();                /* program-specific initialization */
  79.     
  80.     EventLoop();                    /* where it all happens (see below) */
  81.     
  82.     ShutDownEnvironment();            /* where it all ends (see below) */
  83.     
  84.     ExitToShell();
  85. }
  86.  
  87. void EventLoop(void)
  88. {
  89.     while (!gDone)    /* gDone set by choosing "Quit" menu item or by "quit" apple event */
  90.         HandleSingleEvent();
  91. }
  92.  
  93. Boolean HandleSingleEvent(void)
  94. {
  95.     EventRecord        theEvent;
  96.     int                i;
  97.     
  98.     SetCursor(&arrow);        /* should set once every time through event loop */
  99.     HiliteMenu(0);            /* normalize menubar */
  100.     
  101.     gFrontWindowIsOurs=FALSE;
  102.     if (FrontWindow()!=0L)    /* if there's a front window, see if it's one of ours */
  103.     {
  104.         for (i=0; ((i<NUM_WINDOWS) && (!gFrontWindowIsOurs)); i++)
  105.         {
  106.             if (FrontWindow()==gTheWindow[i])    /* found one of ours, so SetPort */
  107.             {
  108.                 SetPort(gTheWindow[i]);
  109.                 gFrontWindowIsOurs=TRUE;
  110.             }
  111.         }
  112.     }
  113.         
  114.     /* get an event from the queue */
  115.     GetTheEvent(&theEvent, gIsInBackground ? gBackgroundWaitTime : gForegroundWaitTime);
  116.     
  117.     DispatchEvents(theEvent);    /* handle the event we just got */
  118.     
  119.     return (theEvent.what!=nullEvent);
  120. }
  121.  
  122. void GetTheEvent(EventRecord *theEvent, int waitTime)
  123. {
  124.     if (gWaitNextEventAvailable)
  125.         WaitNextEvent(everyEvent, theEvent, waitTime, 0L);
  126.     else
  127.     {
  128.         GetNextEvent(everyEvent, theEvent);
  129.         SystemTask();
  130.     }
  131. }
  132.  
  133. void DispatchEvents(EventRecord theEvent)
  134. {
  135.     int                i;
  136.     Point            thisPoint;
  137.     int                index;
  138.     unsigned long    dummy;
  139.     WindowPtr        theWindow;
  140.     Boolean            thisWindowIsOurs;
  141.     ExtendedWindowDataHandle
  142.                     theData;
  143.     
  144.     thisWindowIsOurs=FALSE;
  145.     /* for update/activate events, see if the window in question is one of ours */
  146.     if ((theEvent.what==activateEvt) || (theEvent.what==updateEvt))
  147.     {
  148.         for (i=0; ((i<NUM_WINDOWS) && (!thisWindowIsOurs)); i++)
  149.             thisWindowIsOurs=((WindowPtr)theEvent.message==gTheWindow[i]);
  150.     }
  151.  
  152.     if (thisWindowIsOurs)    /* for activate/update events, get window data structure */
  153.     {
  154.         theData=(WindowDataHandle)GetWRefCon((WindowPtr)theEvent.message);
  155.         index=(**theData).windowIndex;
  156.     }
  157.     else if (gFrontWindowIsOurs)    /* if front window is ours, get window data struct */
  158.     {
  159.         theData=(WindowDataHandle)GetWRefCon(FrontWindow());
  160.         index=(**theData).windowIndex;
  161.     }
  162.     else index=-1;
  163.     
  164.     switch (theEvent.what)
  165.     {
  166.         case nullEvent:    /* ain't nuthin' happenin' */
  167.             if (gFrontWindowIsOurs)        /* give control to window dispatch to handle null */
  168.                 ((**theData).dispatchProc)(theData, kNull, 0L);
  169.             break;
  170.         case mouseDown:    /* mouse button pressed */
  171.             HandleMouseDown(theEvent);    /* see below for mousedown handling */
  172.             break;
  173.         case keyDown:    /* key pressed */
  174.         case autoKey:    /* key help down */
  175.             if (theEvent.modifiers & cmdKey)    /* handle as command-key equivalent */
  176.             {
  177.                 AdjustMenus();    /* just to be safe */
  178.                 /* get the menu ID + item and handle it as a menu choice */
  179.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  180.             }
  181.             else if (gFrontWindowIsOurs)    /* --> window's dispatch for keydown */
  182.                 ((**theData).dispatchProc)(theData, kKeydown, theEvent.message);
  183.             break;
  184.         case diskEvt:    /* disk insert */
  185.             if (HiWord(theEvent.message)!=noErr)    /* bad disk inserted */
  186.             {
  187.                 DILoad();    /* load disk initialization package */
  188.                 SetPt(&thisPoint, 120, 120);
  189.                 DIBadMount(thisPoint, theEvent.message);    /* give format? dialog */
  190.                 DIUnload();    /* unload 'cuz we certainly don't need it */
  191.             }
  192.             break;
  193.         case updateEvt:    /* window update */
  194.             theWindow=(WindowPtr)theEvent.message;    /* which window? */
  195.             
  196.             BeginUpdate(theWindow);        /* means: "OK, we're dealing with this now" */
  197.             
  198.             if (thisWindowIsOurs)        /* one of ours?  see graphics.c */
  199.                 UpdateTheWindow(theData);
  200.             /* if not, could be our progress bar */
  201.             else if ((theWindow!=0L) && (theWindow==gProgressDlog))
  202.                 UpdateDialog(theWindow, theWindow->visRgn);
  203.             
  204.             EndUpdate(theWindow);        /* means: "OK, we're done updating now" */
  205.             break;
  206.         case activateEvt:    /* window activate or deactivate */
  207.             if (thisWindowIsOurs)        /* one of ours?  send message to window dispatch */
  208.                 ((**theData).dispatchProc)(theData,
  209.                     ((theEvent.modifiers&activeFlag)!=0) ? kActivate : kDeactivate, 0L);
  210.             break;
  211.         case osEvt:            /* suspend or resume program execution (switch in/out) */
  212.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  213.             {
  214.                 /* keep track of whether we're in the background or foreground */
  215.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  216.                 
  217.                 if (gFrontWindowIsOurs)        /* send activate/deactivate to front window */
  218.                     ((**theData).dispatchProc)(theData, gIsInBackground ?
  219.                         kDeactivate : kActivate, 0L);
  220.                 
  221.                 for (i=0; i<NUM_WINDOWS; i++)    /* send suspend/resume to all our */
  222.                     if (gTheWindow[i]!=0L)        /* windows that currently exist */
  223.                         ((**(gTheWindowData[i])).dispatchProc)(gTheWindowData[i],
  224.                             gIsInBackground ? kSuspend : kResume, 0L);
  225.                 
  226.                 /* if we just came into the foreground and we have a pending error,
  227.                    now's the time to display it */
  228.                 if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
  229.                 {
  230.                     if (gHasNotificationManager)
  231.                         NMRemove(&gMyNotification);        /* remove notification request */
  232.                     HandleError(gPendingResultCode, FALSE);    /* display alert, see error.c */
  233.                     gPendingResultCode=allsWell;        /* ...now it is */
  234.                 }
  235.             }
  236.             break;
  237.         case kHighLevelEvent:    /* apple event */
  238.             if (gHasAppleEvents)
  239.                 AEProcessAppleEvent(&theEvent);        /* see apple events.c */
  240.             break;
  241.     }
  242. }
  243.  
  244. void HandleMouseDown(EventRecord theEvent)
  245. {
  246.     WindowPtr            theWindow;
  247.     int                    windowCode;
  248.     long                windSize;
  249.     GrafPtr                oldPort;
  250.     int                    i;
  251.     Rect                sizeRect;
  252.     Boolean                gotone;
  253.     int                    index;
  254.     unsigned long        dummy;
  255.     Point                theLocalPoint;
  256.     Boolean                thisWindowIsOurs;
  257.     ExtendedWindowDataHandle    theData;
  258.     
  259.     windowCode=FindWindow(theEvent.where, &theWindow);    /* which window? */
  260.  
  261.     thisWindowIsOurs=FALSE;
  262.     /* find out if the target window was one of ours */
  263.     if (theWindow!=0L)
  264.         for (index=0; ((index<NUM_WINDOWS) && (!thisWindowIsOurs)); index++)
  265.             thisWindowIsOurs=(theWindow==gTheWindow[index]);
  266.  
  267.     if (thisWindowIsOurs)    /* if target window is one of ours, get window data struct */
  268.     {
  269.         theData=(WindowDataHandle)GetWRefCon(theWindow);
  270.         index=(**theData).windowIndex;
  271.     }
  272.     else index=-1;
  273.     
  274.     switch (windowCode)
  275.     {
  276.         case inMenuBar:        /* in menu bar; let system take over */
  277.             AdjustMenus();
  278.             HandleMenu(MenuSelect(theEvent.where));
  279.             break;
  280.         case inContent:        /* in window content */
  281.             if (FrontWindow() != theWindow)        /* maybe switch to different window */
  282.             {
  283.                 if (FrontWindow()==gProgressDlog)    /* but not if progress bar is up */
  284.                     SysBeep(7);
  285.                 else SelectWindow(theWindow);
  286.             }
  287.             else if (gFrontWindowIsOurs)    /* inform window dispatch of mousedown */
  288.             {
  289.                 GetMouse(&theLocalPoint);    /* in window's local coordinates */
  290.                 dummy=theLocalPoint.h;        /* all this fiddling is so we can pass */
  291.                 dummy=dummy<<16;            /* the point (two integers) to the */
  292.                 dummy+=theLocalPoint.v;        /* dispatch procedure in an unsigned long */
  293.                 ((**theData).dispatchProc)(theData, kMousedown, dummy);    /* go for it */
  294.             }
  295.             break;
  296.         case inSysWindow:    /* in system window (desk accessory) */
  297.             SystemClick(&theEvent, theWindow);    /* let the system deal with it */
  298.             break;
  299.         case inDrag:        /* in drag _region_, that is */
  300.             /* the accepted way to draw a window */
  301.             DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
  302.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  303.                 (**theData).windowBounds=(*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  304.             break;
  305.         case inGoAway:        /* close box */
  306.             /* the accepted way to track a close box attempt */
  307.             if (TrackGoAway(theWindow, theEvent.where))
  308.                 DoTheCloseThing(theWindow);        /* see menus.c */
  309.             break;
  310.         case inGrow:        /* grow box */
  311.             /* the accepted way to grow a window */
  312.             sizeRect = screenBits.bounds;
  313.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  314.             
  315.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  316.             if (windSize != 0)
  317.             {
  318.                 GetPort(&oldPort);
  319.                 SetPort(theWindow);
  320.                 EraseRect(&theWindow->portRect);
  321.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  322.                 InvalRect(&theWindow->portRect);
  323.                 SetPort(oldPort);
  324.             }
  325.             
  326.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  327.                 (**theData).windowBounds=(*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  328.             break;
  329.         case inZoomIn:        /* zoom box */
  330.         case inZoomOut:
  331.             /* the accepted way to track a zoom attempt */
  332.             if (TrackBox(theWindow, theEvent.where, windowCode))
  333.             {
  334.                 GetPort(&oldPort);
  335.                 SetPort(theWindow);
  336.                 ZoomWindow(theWindow, windowCode, FALSE);
  337.                 InvalRect(&theWindow->portRect);
  338.                 SetPort(oldPort);
  339.             }
  340.             
  341.             if (thisWindowIsOurs)    /* update window bounds in window data struct */
  342.                 (**theData).windowBounds=(*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  343.             break;
  344.     }
  345. }
  346.  
  347. void ShutDownEnvironment(void)
  348. {
  349.     ShutDownTheProgram();        /* program-specific cleanup */
  350.     ShutDownTheGraphics();        /* shell-specific clean-up */
  351.     ShutDownTheSounds();        /* dispose of sound channel, etc. */
  352. }
  353.